﻿@{
	PageData["Title"] = "Create a new SKU";
    
    WebSecurity.RequireAuthenticatedUser();
    if (!Roles.IsUserInRole("admin")) {
    	Response.Redirect("~");
    }
    
	var name = "";
	var price = 0.0M;
	var description = "";
    var errorMessage = "";
    
    if(!UrlData[0].IsEmpty()) {
        name = UrlData[0];
    }
    
    if(IsPost) {
        name = Request["name"];
        if (name.IsEmpty()) {
            errorMessage = "Name cannot be empty.";
        }
        
        //If no errors in validation, continue with registration
        if (errorMessage.IsEmpty()) {
            price = Request["price"].AsDecimal();
            description = Request["description"];
            try {   //Insert user information into user table                 
                byte[] fileBytes = null;
                HttpPostedFileBase file = Request.Files[0];   
                if (file.ContentLength > 0) {
                    var fileUpload = new WebImage(file.InputStream);
                    fileBytes = fileUpload.GetBytes();
                }
                Freemium.CreateStockKeepingUnit(name, description, price, fileBytes);                
               
                Response.Redirect("~/Freemium/Admin");
            } catch (Exception ex) {
               errorMessage = "Sorry, we ran into a problem processing your request.";
            }
        }        
    }        
}

@if (!errorMessage.IsEmpty()) {
<div class="error">
    @errorMessage
</div>  
}
<h1>SKU Creation</h1>
Use the form below to create a new SKU.<br/>
<div>
    <form id="validateForm" method="post" action="" enctype="multipart/form-data">
	    <fieldset>
		    <legend>Create SKU</legend>
			<div class="freemiumLabel">
				<label class="" for="name">Name</label>
			</div>
			<div class="freemiumInput">
                @if (!name.IsEmpty()) {
                    <input name="name" type="hidden" value="@name" />
                    <input name="name" type="text" value="@name" disabled />
                } else {
				    <input id="name" name="name" type="text" maxlength="128" value="@name" />
                }
			</div>
			<div class="freemiumLabel">
				<label for="price">Price</label>
			</div>
			<div class="freemiumInput">
				<input id="price" name="price" type="text" maxlength="5" value="@price" />
			</div>	
			<div class="freemiumLabel">
				<label for="description">Description</label>
			</div>
			<div class="freemiumInput">
				<input id="description" name="description" type="text" maxlength="1000" />
			</div>
            
            <h2>Image</h2>
            @FileUpload.GetHtml(allowMoreFilesToBeAdded:false, includeFormTag:false)

            <div class="submit-group">
                <input type="submit" value="Create" />
                <input type="reset" value="Reset" />
                <input type="button" value="Cancel" name="action" onclick="javascript:window.location='@Href("~/Freemium/Admin")'" />
            </div>
        </fieldset>
    </form>
</div>